package eu.dominicum.ft.pd.boundary.ejb;

import eu.dominicum.ft.pd.Country;
import eu.dominicum.ft.pd.Person;
import eu.dominicum.ft.pd.ProfessionalCyclist;
import eu.dominicum.ft.pd.boundary.GeneralManagement;
import eu.dominicum.ft.pd.mao.MakePersonMAO;
import eu.dominicum.ft.pd.mao.MakeProfessionalCyclistMAO;

import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.validation.ValidationException;
import java.util.List;

/**
 * An EJB implementation of the {@link GeneralManagement} interface.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class GeneralManagementEJB implements GeneralManagement {

    /**
     * The entity manager.
     */
    @PersistenceContext(name = "pu_ft")
    private EntityManager entityManager;

    @Override
    public List<ProfessionalCyclist> findAllProfessionalCyclists() {

        Query namedQuery = entityManager.createNamedQuery("ProfessionalCyclist.findAll", ProfessionalCyclist.class);
        return namedQuery.getResultList();
    }

    @Override
    public void changeProfessionalCyclistNationality(final Long professionalCyclistId, final Long newNationalityId) {

        ProfessionalCyclist professionalCyclist = this.entityManager.find(ProfessionalCyclist.class, professionalCyclistId);
        Country nationality = this.entityManager.find(Country.class, newNationalityId);
        professionalCyclist.changeNationality(nationality);
        this.entityManager.merge(professionalCyclist);

    }

    @Override
    public Person makePerson(final MakePersonMAO makePersonMAO) {

        try {
            Person newPerson = Person.make(makePersonMAO);
            this.entityManager.persist(newPerson);

            return newPerson;
        } catch (ValidationException vex) {
            throw new EJBException(vex.getMessage());
        }
    }

    @Override
    public List<Person> findAllPerson() {

        Query namedQuery = entityManager.createNamedQuery("Person.findAll", Person.class);
        return namedQuery.getResultList();
    }

    @Override
    public ProfessionalCyclist makeProfessionalCyclist(final MakeProfessionalCyclistMAO makeProfessionalCyclistMAO) {

        if (makeProfessionalCyclistMAO.getPersonId() != null) {
            makeProfessionalCyclistMAO.setPerson(this.entityManager.find(Person.class, makeProfessionalCyclistMAO.getPersonId()));
        }

        if (makeProfessionalCyclistMAO.getCountryId() != null) {
            makeProfessionalCyclistMAO.setNationality(this.entityManager.find(Country.class, makeProfessionalCyclistMAO.getCountryId()));
        }

        try {
            ProfessionalCyclist newProfessionalCyclist = ProfessionalCyclist.make(makeProfessionalCyclistMAO);
            this.entityManager.persist(newProfessionalCyclist);

            return newProfessionalCyclist;
        } catch (ValidationException vex) {
            throw new EJBException(vex.getMessage());
        }
    }
}
